layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 对接第三方接口的配置管理
     */
    var ThirdInterfaceConfig = {
        tableId: "thirdInterfaceConfigTable"
    };

    /**
     * 初始化表格的列
     */
    ThirdInterfaceConfig.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {type: 'numbers', title: '序号', width: 80},
            {field: 'status', sort: true, title: '状态:', templet: ThirdInterfaceConfig.formatStatus},
            {field: 'regionId', sort: true, title: '地域ID', width: 160},
            {field: 'accessKeyId', sort: true, title: '秘钥ID', width: 160},
            {field: 'accessKeySecret', sort: true, title: '秘钥Key', width: 160},
            {field: 'url', sort: true, title: '访问Url', width: 160},
            {field: 'signName', sort: true, title: '签名名称', width: 160},
            {field: 'templateCode', sort: true, title: '模板code', width: 160},
            {field: 'code', sort: true, title: '类型代码', width: 180},
            {field: 'remarks', sort: true, title: '备注', width: 160},
            {field: 'accessToken', sort: true, title: '访问令牌', width: 160},
            {field: 'createTime', sort: true, title: '创建时间', width: 160},
            {field: 'updateTime', sort: true, title: '修改时间', width: 160},

            /*        {field: 'updateUser', sort: true, title: '修改用户'},*/
            // {field: 'deleteFlag', sort: true, title: '删除标记 0:正常 1:删除'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 160}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ThirdInterfaceConfig.search = function () {
        var queryData = getParam();

        table.reload(ThirdInterfaceConfig.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 获取查询参数
     */
    function getParam() {
        var queryData = {};
        queryData['createTime'] = $('#createTime').val();
        queryData['status'] = $('input[name="status"] option:checked').val();
        return queryData;
    }

    /**
     * 弹出添加对话框
     */
    ThirdInterfaceConfig.openAddDlg = function () {
        func.open({
            title: '接口配置添加',
            content: Changein.ctxPath + '/thirdInterfaceConfig/add',
            tableId: ThirdInterfaceConfig.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ThirdInterfaceConfig.openEditDlg = function (data) {
        func.open({
            title: '接口配置修改',
            content: Changein.ctxPath + '/thirdInterfaceConfig/edit?id=' + data.id,
            tableId: ThirdInterfaceConfig.tableId
        });
    };

    ThirdInterfaceConfig.formatStatus = function formatStatus(d) {
        //状态
        if (!d.status) return "";
        let checkedStatus = d.status == 1 ? 'checked' : '';
        return "<input type='checkbox'  " + checkedStatus + " value='" + d.status + "' lay-skin='switch' lay-text='启用|禁用' lay-filter='statusOptFilter' successId='" + d.id + "'>";
    }

    /**
     * 删除
     */
    var deleteBatchLock = false;
    ThirdInterfaceConfig.deleteBatch = function () {
        if (!deleteBatchLock) {
            var checkRows = table.checkStatus(ThirdInterfaceConfig.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要删除的数据");
                return false;
            }
            var arr = [];
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit != 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部已审核，请选择未审核的数据");
                return false;
            }
            var operation = function () {
                deleteBatchLock = true;
                var ajax = new $ax(Changein.ctxPath + "/thirdInterfaceConfig/batchDeleteByIds", function (data) {
                    if (data.code != 200) {
                        Changein.error("删除失败！" + data.message);
                    } else {
                        Changein.success("删除成功！");
                    }
                    deleteBatchLock = false;
                    table.reload(ThirdInterfaceConfig.tableId);
                }, function (data) {
                    deleteBatchLock = false;
                    Changein.error("删除失败！" + data.responseJSON.message)
                });
                ajax.set({"ids": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };

    /**
     * 审核
     */
    var auditLock = false;
    ThirdInterfaceConfig.auditBatch = function () {
        if (!auditLock) {
            var arr = [];
            var checkRows = table.checkStatus(ThirdInterfaceConfig.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要审核的数据");
                return false;
            }
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit != 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部已审核");
                return false;
            }
            var operation = function () {
                auditLock = true;
                var ajax = new $ax(Changein.ctxPath + "/thirdInterfaceConfig/setAudit", function (data) {
                    if (data.code != 200) {
                        Changein.error("审核失败！" + data.message);
                    } else {
                        Changein.success("审核成功！");
                    }
                    auditLock = false;
                    table.reload(ThirdInterfaceConfig.tableId);
                }, function (data) {
                    auditLock = false;
                    Changein.error("审核失败！" + data.responseJSON.message)
                });
                ajax.set({"id": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否审核?", operation);
        } else {
            Changein.error("正在审核中");
        }
    };

    /**
     * 反审核
     */
    var unAuditLock = false;
    ThirdInterfaceConfig.unAuditBatch = function () {
        if (!unAuditLock) {
            var arr = [];
            var checkRows = table.checkStatus(ThirdInterfaceConfig.tableId);
            if (checkRows.data.length == 0) {
                Changein.error("请选择要反审核的数据");
                return false;
            }
            var tabData = checkRows.data;
            for (var i = 0; i < tabData.length; i++) {
                if (tabData[i].audit == 1) {
                    arr.push(tabData[i].id);
                }
            }
            if (arr.length == 0) {
                Changein.error("选中数据全部未审核");
                return false;
            }
            var operation = function () {
                unAuditLock = true;
                var ajax = new $ax(Changein.ctxPath + "/thirdInterfaceConfig/setUnAudit", function (data) {
                    if (data.code != 200) {
                        Changein.error("反审核失败！" + data.message);
                    } else {
                        Changein.success("反审核成功！");
                    }
                    unAuditLock = false;
                    table.reload(ThirdInterfaceConfig.tableId);
                }, function (data) {
                    unAuditLock = false;
                    Changein.error("反审核失败！" + data.responseJSON.message)
                });
                ajax.set({"id": arr.toString()});
                ajax.start();
            }
            Changein.confirm("是否反审核?", operation);
        } else {
            Changein.error("正在反审核中");
        }
    };


    /**
     * 导出excel按钮,EXPORT_CODE需要在开发设置-导出设置里配置
     */
    ThirdInterfaceConfig.exportExcel = function () {
        Changein.exportData(tableResult.config.id, "EXPORT_CODE", table, getParam());
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ThirdInterfaceConfig.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Changein.ctxPath + "/thirdInterfaceConfig/delete", function (data) {
                Changein.success("删除成功!");
                table.reload(ThirdInterfaceConfig.tableId);
            }, function (data) {
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Changein.confirm("是否删除?", operation);
    };
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    var deleteLock = false;
    ThirdInterfaceConfig.onDeleteItem = function (data) {
        if (data.audit == 1) {
            Changein.error("选中数据已审核，请选择未审核的数据");
            return false;
        }
        var operation = function () {
            deleteLock = true;
            var ajax = new $ax(Changein.ctxPath + "/thirdInterfaceConfig/delete", function (data) {
                deleteLock = false;
                if (data.code != 200) {
                    Changein.error("删除失败！" + data.message);
                } else {
                    Changein.success("删除成功！");
                }
                table.reload(ThirdInterfaceConfig.tableId);
            }, function (data) {
                deleteLock = false;
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        if (!deleteLock) {
            Changein.confirm("是否删除?", operation);
        } else {
            Changein.error("正在删除中");
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ThirdInterfaceConfig.tableId,
        url: Changein.ctxPath + '/thirdInterfaceConfig/list',
        page: true,
        height: "full-130",
        cellMinWidth: 100,
        cols: ThirdInterfaceConfig.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ThirdInterfaceConfig.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ThirdInterfaceConfig.openAddDlg();
    });
    // 删除
    $("#btnDel").click(function () {
        ThirdInterfaceConfig.deleteBatch();
    });

    //审核
    $('#btnAudit').click(function () {
        ThirdInterfaceConfig.auditBatch();
    });

    //反审核
    $('#btnUnAudit').click(function () {
        ThirdInterfaceConfig.unAuditBatch();
    });
    // 导出excel
    $('#btnExp').click(function () {
        var checkRows = table.checkStatus(ThirdInterfaceConfig.tableId);
        if (checkRows.data.length === 0) {
            ThirdInterfaceConfig.exportExcel();
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }

    });
    //下载模板,TEMPLATE_CODE需要在开发设置-模板管理里配置
    $("#btnDownloadTemplate").click(function () {
        Changein.downLoadTempate("TEMPLATE_CODE");
    });
    // 工具条点击事件
    table.on('tool(' + ThirdInterfaceConfig.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ThirdInterfaceConfig.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ThirdInterfaceConfig.onDeleteItem(data);
        }
    });
});
